# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;

#worker_connections(1014);
#master_process_enabled(1);
#log_level('warn');

log_level('debug');

repeat_each(2);

plan tests => repeat_each() * (blocks() * 2 + 13);

#no_diff();
#no_long_string();

run_tests();

__DATA__

=== TEST 1: set response content-type header
--- config
    location /read {
        echo "Hi";
        header_filter_by_lua '
            njt.header.content_type = "text/my-plain";
        ';

    }
--- request
GET /read
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 2: server config
--- config
    header_filter_by_lua '
        njt.header.content_type = "text/my-plain";
    ';

    location /read {
        echo "Hi";

    }
--- request
GET /read
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 3: set in http
--- http_config
    header_filter_by_lua '
        njt.header.content_type = "text/my-plain";
    ';
--- config
    location /read {
        echo "Hi";
    }
--- request
GET /read
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 4: overriding config
--- config
    header_filter_by_lua '
        njt.header.content_type = "text/my-plain";
    ';
    location /read {
        echo "Hi";
        header_filter_by_lua '
            njt.header.content_type = "text/read-plain";
        ';
    }
--- request
GET /read
--- response_headers
Content-Type: text/read-plain
--- response_body
Hi



=== TEST 5: set response content-type header
--- config
    location /read {
        echo "Hi";
        header_filter_by_lua '
            njt.header.content_type = "text/my-plain";
        ';

    }
--- request
GET /read
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 6: lua code run failed
--- config
    location /read {
        echo "Hi";
        header_filter_by_lua '
            njt.header.content_length = "text/my-plain";
        ';
    }
--- request
GET /read
--- error_code
--- response_body



=== TEST 7: use variable generated by content phrase
--- config
   location /read {
        set $strvar '1';
        content_by_lua '
            njt.var.strvar = "127.0.0.1:8080";
            njt.say("Hi");
        ';
        header_filter_by_lua '
            njt.header.uid = njt.var.strvar;
        ';
    }
--- request
GET /read
--- response_headers
uid: 127.0.0.1:8080
--- response_body
Hi



=== TEST 8: use variable generated by content phrase for HEAD
--- config
   location /read {
        set $strvar '1';
        content_by_lua '
            njt.var.strvar = "127.0.0.1:8080";
            njt.say("Hi");
        ';
        header_filter_by_lua '
            njt.header.uid = njt.var.strvar;
        ';
    }
--- request
HEAD /read
--- response_headers
uid: 127.0.0.1:8080
--- response_body



=== TEST 9: use variable generated by content phrase for HTTP 1.0
--- config
   location /read {
        set $strvar '1';
        content_by_lua '
            njt.var.strvar = "127.0.0.1:8080";
            njt.say("Hi");
        ';
        header_filter_by_lua '
            njt.header.uid = njt.var.strvar;
        ';

    }
--- request
GET /read HTTP/1.0
--- response_headers
uid: 127.0.0.1:8080
--- response_body
Hi



=== TEST 10: use capture and header_filter_by
--- config
   location /sub {
        content_by_lua '
            njt.say("Hi");
        ';
        header_filter_by_lua '
            njt.header.uid = "sub";
        ';
    }

    location /parent {
        content_by_lua '
            local res = njt.location.capture("/sub")
            if res.status == 200 then
                njt.say(res.header.uid)
            else
                njt.say("parent")
            end
        ';
        header_filter_by_lua '
            njt.header.uid = "parent";
        ';
    }

--- request
GET /parent
--- response_headers
uid: parent
--- response_body
sub



=== TEST 11: overriding ctx
--- config
    location /lua {
        content_by_lua '
            njt.ctx.foo = 32;
            njt.say(njt.ctx.foo)
        ';
        header_filter_by_lua '
            njt.ctx.foo = njt.ctx.foo + 1;
            njt.header.uid = njt.ctx.foo;
        ';
    }
--- request
GET /lua
--- response_headers
uid: 33
--- response_body
32



=== TEST 12: use req
--- config
    location /lua {
        content_by_lua '
            njt.say("Hi");
        ';

        header_filter_by_lua '
            local str = "";
            local args, err = njt.req.get_uri_args()
            if err then
                njt.log(njt.ERR, "err: ", err)
                return njt.exit(500)
            end
            local keys = {}
            for key, val in pairs(args) do
                table.insert(keys, key)
            end
            table.sort(keys)
            for i, key in ipairs(keys) do
                local val = args[key]
                if type(val) == "table" then
                    str = str .. table.concat(val, ", ")
                else
                    str = str .. ":" .. val
                end
            end

            njt.header.uid = str;
        ';
    }
--- request
GET /lua?a=1&b=2
--- response_headers
uid: :1:2
--- response_body
Hi



=== TEST 13: use njt md5 function
--- config
    location /lua {
        content_by_lua '
            njt.say("Hi");
        ';
        header_filter_by_lua '
            njt.header.uid = njt.md5("Hi");
        ';
    }
--- request
GET /lua
--- response_headers
uid: c1a5298f939e87e8f962a5edfc206918
--- response_body
Hi



=== TEST 14: set response content-type header (by file)
--- config
    location /read {
        echo "Hi";
        header_filter_by_lua_file 'html/foo.lua';
    }
--- request
GET /read
--- user_files
>>> foo.lua
njt.header.content_type = "text/my-plain";
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 15: by_lua_file server config
--- config
    header_filter_by_lua_file 'html/foo.lua';

    location /read {
        echo "Hi";
    }
--- request
GET /read
--- user_files
>>> foo.lua
njt.header.content_type = "text/my-plain";
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 16: by_lua_file set in http
--- http_config
    header_filter_by_lua_file 'html/foo.lua';
--- config
    location /read {
        echo "Hi";
    }
--- request
GET /read
--- user_files
>>> foo.lua
njt.header.content_type = "text/my-plain";
--- response_headers
Content-Type: text/my-plain
--- response_body
Hi



=== TEST 17: by_lua_file overriding config
--- config
    header_filter_by_lua 'html/foo.lua';
    location /read {
        echo "Hi";
        header_filter_by_lua_file 'html/bar.lua';
    }
--- request
GET /read
--- user_files
>>> foo.lua
njt.header.content_type = "text/my-plain";
>>> bar.lua
njt.header.content_type = "text/read-plain";
--- response_headers
Content-Type: text/read-plain
--- response_body
Hi



=== TEST 18: njt.ctx available in header_filter_by_lua (already defined)
--- config
    location /lua {
        content_by_lua 'njt.ctx.counter = 3 njt.say(njt.ctx.counter)';
        header_filter_by_lua 'njt.log(njt.ERR, "njt.ctx.counter: ", njt.ctx.counter)';
    }
--- request
GET /lua
--- response_body
3
--- error_log
njt.ctx.counter: 3
lua release njt.ctx



=== TEST 19: njt.ctx available in header_filter_by_lua (not defined yet)
--- config
    location /lua {
        echo hello;
        header_filter_by_lua '
            njt.log(njt.ERR, "njt.ctx.counter: ", njt.ctx.counter)
            njt.ctx.counter = "hello world"
        ';
    }
--- request
GET /lua
--- response_body
hello
--- error_log
njt.ctx.counter: nil
lua release njt.ctx



=== TEST 20: globals are shared by all requests
--- config
    location /lua {
        set $foo '';
        content_by_lua '
            njt.send_headers()
            njt.say(njt.var.foo)
        ';
        header_filter_by_lua '
            if not foo then
                foo = 1
            else
                njt.log(njt.INFO, "old foo: ", foo)
                foo = foo + 1
            end
            njt.var.foo = foo
        ';
    }
--- request
GET /lua
--- response_body_like
^[12]$
--- no_error_log
[error]
--- grep_error_log eval: qr/old foo: \d+/
--- grep_error_log_out eval
["", "old foo: 1\n"]



=== TEST 21: lua error (string)
--- config
    location /lua {
        set $foo '';
        content_by_lua '
            njt.send_headers()
            njt.say(njt.var.foo)
        ';
        header_filter_by_lua '
            error("Something bad")
        ';
    }
--- request
GET /lua
--- ignore_response
--- error_log
failed to run header_filter_by_lua*: header_filter_by_lua:2: Something bad
--- no_error_log
[alert]



=== TEST 22: lua error (nil)
--- config
    location /lua {
        set $foo '';
        content_by_lua '
            njt.send_headers()
            njt.say(njt.var.foo)
        ';
        header_filter_by_lua '
            error(nil)
        ';
    }
--- request
GET /lua
--- ignore_response
--- error_log
failed to run header_filter_by_lua*: unknown reason
--- no_error_log
[alert]



=== TEST 23: no njt.print
--- config
    location /lua {
        header_filter_by_lua "njt.print(32) return 1";
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 24: no njt.say
--- config
    location /lua {
        header_filter_by_lua "njt.say(32) return 1";
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 25: no njt.flush
--- config
    location /lua {
        header_filter_by_lua "njt.flush()";
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 26: no njt.eof
--- config
    location /lua {
        header_filter_by_lua "njt.eof()";
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 27: no njt.send_headers
--- config
    location /lua {
        header_filter_by_lua "njt.send_headers()";
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 28: no njt.location.capture
--- config
    location /lua {
        header_filter_by_lua 'njt.location.capture("/sub")';
        echo ok;
    }

    location /sub {
        echo sub;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 29: no njt.location.capture_multi
--- config
    location /lua {
        header_filter_by_lua 'njt.location.capture_multi{{"/sub"}}';
        echo ok;
    }

    location /sub {
        echo sub;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 30: no njt.redirect
--- config
    location /lua {
        header_filter_by_lua 'njt.redirect("/blah")';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 31: no njt.exec
--- config
    location /lua {
        header_filter_by_lua 'njt.exec("/blah")';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 32: no njt.req.set_uri(uri, true)
--- config
    location /lua {
        header_filter_by_lua 'njt.req.set_uri("/blah", true)';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 33: njt.req.set_uri(uri) exists
--- config
    location /lua {
        header_filter_by_lua 'njt.req.set_uri("/blah") return 1';
        content_by_lua '
            njt.send_headers()
            njt.say("uri: ", njt.var.uri)
        ';
    }
--- request
GET /lua
--- response_body
uri: /blah
--- no_error_log
[error]



=== TEST 34: no njt.req.read_body()
--- config
    location /lua {
        header_filter_by_lua 'njt.req.read_body()';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 35: no njt.req.socket()
--- config
    location /lua {
        header_filter_by_lua 'return njt.req.socket()';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 36: no njt.socket.tcp()
--- config
    location /lua {
        header_filter_by_lua 'return njt.socket.tcp()';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 37: no njt.socket.connect()
--- config
    location /lua {
        header_filter_by_lua 'return njt.socket.connect("127.0.0.1", 80)';
        echo ok;
    }
--- request
GET /lua
--- ignore_response
--- error_log
API disabled in the context of header_filter_by_lua*



=== TEST 38: clear content-length
--- config
    location /lua {
        content_by_lua '
            njt.header.content_length = 12
            njt.say("hello world")
        ';
        header_filter_by_lua 'njt.header.content_length = nil';
    }
--- request
GET /lua
--- response_headers
!content-length
--- response_body
hello world



=== TEST 39: backtrace
--- config
    location /t {
        header_filter_by_lua '
            local bar
            local function foo()
                bar()
            end

            function bar()
                error("something bad happened")
            end

            foo()
        ';
        echo ok;
    }
--- request
    GET /t
--- ignore_response
--- error_log
something bad happened
stack traceback:
in function 'error'
in function 'bar'
in function 'foo'



=== TEST 40: Lua file does not exist
--- config
    location /lua {
        echo ok;
        header_filter_by_lua_file html/test2.lua;
    }
--- user_files
>>> test.lua
v = njt.var["request_uri"]
njt.print("request_uri: ", v, "\n")
--- request
GET /lua?a=1&b=2
--- ignore_response
--- error_log eval
qr/failed to load external Lua file ".*?test2\.lua": cannot open .*? No such file or directory/



=== TEST 41: filter finalize
--- config
    error_page 582 = /bar;
    location = /t {
        echo ok;
        header_filter_by_lua '
            return njt.exit(582)
        ';
    }

    location = /bar {
        echo hi;
        header_filter_by_lua '
            return njt.exit(302)
        ';
    }
--- request
GET /t
--- response_body_like: 302 Found
--- error_code: 302
--- no_error_log
[error]
